package com.kooison.trafficMonitor.crawler;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

import javax.xml.stream.XMLEventReader;

import com.kooison.trafficMonitor.LocationInfo;
import com.kooison.util.properties.PropertyLoader;

public class Crawler {
	private static Map map;
	private static Properties mProperties;
	
	private static final Logger log = Logger.getLogger(Crawler.class.getName());
	
	/*
	private String user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
	private String target_charset = "utf-8";
	private String target_language = "zh-TW";
	private String target_url = "http://traffic.td.gov.hk/ClickProcessor.do?areaID=hk&url=pdasnapshot&dot_id=011124";
	private String target_pattern = "time=";
	private String target_list_url = "http://traffic.td.gov.hk/ClickProcessor.do?url=pdaarea&area_id=";
	private String target_list_pattern = "dot_id=";
	private String target_img_path = "http://traffic.td.gov.hk/DisplayImage.do?pda=1&id={0}&time=";
	private String area_id_list = "hk,kl,tw,tm,yl,st,li";
	private String area_name_list = "\u9999\u6e2f\u5cf6,\u4e5d\u9f8d,\u8343\u7063,\u5c6f\u9580,\u5143\u6717\u3001\u7c89\u5dba\u53ca\u5927\u57d4,\u6c99\u7530\u53ca\u99ac\u978d\u5c71,\u5927\u5dbc\u5c71";
	*/
	
	private String user_agent;
	private String target_charset;
	private String target_language;
	private String target_url;
	private String target_pattern;
	private String target_list_url;
	private String target_list_pattern;
	private String target_img_path;
	private String area_id_list;
	private String area_name_list;
	
	/*
	public void setUser_agent(String userAgent) {
		user_agent = userAgent;
	}

	public void setTarget_charset(String targetCharset) {
		target_charset = targetCharset;
	}

	public void setTarget_language(String targetLanguage) {
		target_language = targetLanguage;
	}

	public void setTarget_url(String targetUrl) {
		target_url = targetUrl;
	}

	public void setTarget_pattern(String targetPattern) {
		target_pattern = targetPattern;
	}

	public void setTarget_list_url(String targetListUrl) {
		target_list_url = targetListUrl;
	}

	public void setTarget_list_pattern(String targetListPattern) {
		target_list_pattern = targetListPattern;
	}

	public void setTarget_img_path(String targetImgPath) {
		target_img_path = targetImgPath;
	}

	public void setArea_id_list(String areaIdList) {
		area_id_list = areaIdList;
	}

	public void setArea_name_list(String areaNameList) {
		area_name_list = areaNameList;
	}
	*/

	public Map getMap() {
		if (map == null) {
			map = getList();
		}
		return map;
	}
	
	public Crawler() {
		log.info("loading config...");
		mProperties = PropertyLoader.loadProperties("config.properties");
		user_agent = mProperties.getProperty("user_agent");
		target_charset = mProperties.getProperty("target_charset");
		target_language = mProperties.getProperty("target_language");
		target_url = mProperties.getProperty("target_url");
		target_pattern = mProperties.getProperty("target_pattern");
		target_list_url = mProperties.getProperty("target_list_url");
		target_list_pattern = mProperties.getProperty("target_list_pattern");
		target_img_path = mProperties.getProperty("target_img_path");
		area_id_list = mProperties.getProperty("area_id_list");
		area_name_list = mProperties.getProperty("area_name_list");
		log.info("finished");
	}

	private HttpURLConnection getConnection(String target) throws Exception {
		HttpURLConnection conn = (HttpURLConnection) new URL(target).openConnection();
		conn.setConnectTimeout(60000); // 60 sec
		conn.setReadTimeout(60000); // 60 sec
		conn.setInstanceFollowRedirects(true);
		conn.setRequestProperty("User-agent", user_agent);
		conn.setRequestProperty("Accept-Language", target_language);
		conn.setRequestProperty("Accept-Charset", target_charset);
		return conn;
	}
	
	private Map getList() {
		String line = null;

		try {
			String[] area_ids = area_id_list.split(",");
			String[] area_names = area_name_list.split(",");

			Map area_dotList_map = new HashMap();

			int area_cnt = 0;
			for (String id : area_ids) {
				HttpURLConnection conn = getConnection(target_list_url + id);

				BufferedReader br;
				br = new BufferedReader(new InputStreamReader(conn.getInputStream(), target_charset));

				List<LocationInfo> dotList = new ArrayList<LocationInfo>();

				// Update the content
				while ((line = br.readLine()) != null) {
					int i = -1;
					if ((i = line.indexOf(target_list_pattern)) > -1) {
						log.info(line);
						
						LocationInfo loc = new LocationInfo();
								
						// set area
						loc.setAreaId(id);
						loc.setAreaName(area_names[area_cnt]);
						
						// get dot id
						int j = line.indexOf("\"", i);
						String dotid = line.substring(i + target_list_pattern.length(), j);
						loc.setDotId(dotid);
						loc.setImgPath(MessageFormat.format(target_img_path, new String[]{dotid}));
						
						// get name
						int k = line.indexOf("</a>", j);
						String name = line.substring(j + 2, k);
						loc.setDotName(name);
						
						log.info(loc.toString());
						dotList.add(loc);
						// return line;
					}
				}
				area_dotList_map.put(id, dotList);
				area_cnt++;
			}
			
			return area_dotList_map;
			/*
			JSONObject object = new JSONObject();
			try {
				object.put("map", area_dotList_map);
				System.out.println(object.toString());
				return object.toString();
			} catch (JSONException e) {
			}
			*/
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String getTime() {
		String line = null;
	
		try {
			HttpURLConnection conn = getConnection(target_url);

			BufferedReader br;
			br = new BufferedReader(new InputStreamReader(conn.getInputStream(), target_charset));

			// Update the content
			while ((line = br.readLine()) != null) {
				int i = -1;
				if ((i = line.indexOf(target_pattern)) > -1) {
					log.info(line);
					int j = line.indexOf("\"", i);
					line = line.substring(i + target_pattern.length(), j);
					log.info(line);
					return line;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}
